package com.yunling.qishi.macaca.utils;
/**
 * 提供ui测试中操作方法
 * @author Administrator
 *
 */

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.testng.Assert;

import macaca.client.MacacaClient;
import macaca.client.commands.Element;

public class MacacaUtil {

	private MacacaClient driver;
	private Element element;
	private boolean flag = false;
	private String destFile;// 用于存储截图存放位置
	private String elementFile;//
	private static final int locateStyleIndex = 2;
	private static final int locateExpressIndex = 3;
	private static final int pageNameIndex = 0;
	private static final int elementNameIndex = 0;

	public MacacaUtil(MacacaClient driver) {
		this.driver = driver;
	}

	public MacacaClient getDriver() {
		return driver;
	}

	public void setDriver(MacacaClient driver) {
		this.driver = driver;
	}

	public Element getElement() {
		return element;
	}

	public void setElement(Element element) {
		this.element = element;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public String getDestFile() {
		return destFile;
	}

	public void setDestFile(String destFile) {
		this.destFile = destFile;
	}

	/**
	 * 获取元素excel定位方式，并放到集合用map<pagename_elementname,list<elementsInfo(locateMethod,locateExpression)>>>
	 */
	/*
	 * 所有动作分成两种，一种是根据坐标再操作，一种是根据元素是来操作。坐标操作时将元素设为空，其他元素可正常操作
	 * 
	 */
	public MacacaUtil click(Enum element) {
		/*
		 * 判断是tap 还是定位元素
		 */
		/*
		 * 1.获取元素集合 2.判断方式
		 */
		List<String> elelist = ElementsPool.getElementPool().getElements(element);
		String locateStyle = elelist.get(locateStyleIndex);
		try {
			if ("tap".equals(locateStyle.toLowerCase())) {
				this.element = null;
				
				return tapElement(elelist);
			} else {
				this.element = getLocator(elelist);
				this.element.click();
				driver.sleep(3000);
				return this;
			}
			
		} catch (Exception e) {

			e.printStackTrace();
		}

		return this;
	}
	
	/**
	 * sendkeys
	 */
	public MacacaUtil sendkeys(Enum elementName,String content){
		List<String> elelist = ElementsPool.getElementPool().getElements(elementName);
		this.element = getLocator(elelist);
		try {
			this.element.sendKeys(content);
			driver.sleep(3000);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return this;
		
	}
	
	/**
	 * 回退
	 */
	public MacacaUtil back(){
		try {
			driver.back();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this;
	}
	

	/**
	 * 用来计算tap的坐标
	 */
	private MacacaUtil tapElement(List<String> list) {
		String string = list.get(locateExpressIndex);
		String[] split = string.split(",");// 0为宽，1为高
		String[] widths = split[0].split("/");
		String[] heighs = split[1].split("/");
		try {
			driver.tap(getWindowWidth() * Double.valueOf(widths[0]) / Double.valueOf(widths[1]),
					getWindowHeight() * Double.valueOf(heighs[0]) / Double.valueOf(heighs[1]));
			driver.sleep(3000);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this;

	}

	/**
	 * 获取手机屏幕宽度
	 */
	public int getWindowWidth() {
		try {
			return driver.getWindowSize().getIntValue("width");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 获取手机屏幕高度
	 */
	public int getWindowHeight() {

		try {
			return driver.getWindowSize().getIntValue("height");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * 等待时间
	 * @param time
	 * @return
	 */
	public MacacaUtil sleep(int time){
		try {
			driver.sleep(time);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this;
	}
	
	/**
	 * 截图
	 */
	public MacacaUtil takeScreenShot(){
		filepath();
		
		try {
			driver.saveScreenshot(destFile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this;
	}
	
	private void filepath(){
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
		Date date=new Date();
		String string = simpleDateFormat.format(date);
		simpleDateFormat=new SimpleDateFormat("HH-mm-ss-SSS");
		String string2 = simpleDateFormat.format(date);
		destFile="screenshot"+File.separator+string+File.separator+string2+".png";
		destFile=System.getProperty("user.dir")+File.separator+destFile;
		File file=new File(destFile);
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		//StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();	
	}
	
	 
	/**
	 * 获取页面资源
	 */
	public String source(){
		try {
			return driver.source();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "-1";
	}
	/**
	 * 断言交易金额是否正确，需要连数据库
	 * @param cashnum
	 */
	public void assertionDB(int cashnum){
		Assert.assertTrue(cashnum==cashnum);
	}
	
	
	
	
	
	/**
	 * 横向滑屏 
	 * 
	 */
	
	
	
	
	
	/**
	 * 纵向滑屏
	 */
	
	
	/**
	 * 判断每一条元素定位信息的定位方法和定位代码，并返回定位String对像,
	 * 
	 * @param list
	 * @return
	 */
	private Element getLocator(List<String> list) {
		String locatorMethod = list.get(locateStyleIndex);// 获取定位方法
		String locatorCode = list.get(locateExpressIndex);// 获取定位代码
		try {

			if (locatorMethod.toLowerCase().equals("id")) {
				return driver.waitForElementById(locatorCode);
			} else if (locatorMethod.toLowerCase().equals("linktext")) {
				return driver.waitForElementByLinkText(locatorCode);
			} else if (locatorMethod.toLowerCase().equals("partiallinktext")) {
				return driver.waitForElementByPartialLinkText(locatorCode);
			} else if (locatorMethod.toLowerCase().equals("name")) {
				return driver.waitForElementByName(locatorCode);
			} else if (locatorMethod.toLowerCase().equals("tagname")) {
				return driver.waitForElementByTagName(locatorCode);
			} else if (locatorMethod.toLowerCase().equals("xpath")) {
				return driver.waitForElementByXPath(locatorCode);
			} else if (locatorMethod.toLowerCase().equals("cssselector") || locatorMethod.toLowerCase().equals("css")) {
				return driver.waitForElementByCss(locatorCode);
			} else {
				Log.error("元素定位名不正确");
				return null;
			}
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
		
	}

}
